/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.knezevic.realestate.hibernate;

import com.knezevic.realestate.RealEstate;
import com.knezevic.realestate.beans.RealEstateBean;
import com.knezevic.realestate.service.RealEstates;
import java.io.Serializable;
import java.util.Collection;
import org.hibernate.Criteria;
import org.hibernate.Session;

/**
 *
 * @author Nemanja
 */
public class RealEstatesServiceHibernate implements RealEstates{

    private final Session session;

    public RealEstatesServiceHibernate(Session session) {
        this.session = session;
    }
    

    @Override
    public RealEstate save(RealEstate realEstate) {
        return (RealEstate) session.get(RealEstateBean.class, session.save(realEstate));
    }

    @Override
    public void update(RealEstate realEstate) {
        session.merge(realEstate);
        
//        session.saveOrUpdate(realEstate);
    }

    @Override
    public void deleteRealEstate(RealEstate realEstate) {
        session.delete(realEstate);
    }

    @Override
    public Collection<RealEstate> retrieve() {
        Criteria criteria = session.createCriteria(RealEstateBean.class);
        return criteria.list();
    }

    @Override
    public RealEstate retrieve(Serializable id) {
        return (RealEstate) session.get(RealEstateBean.class, id);
    }
}
